package com.example.mq;

import com.example.constant.CommentConstant;
import com.example.entity.OrderInfo;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description: 邮件消费者
 * @Author: mingtian
 * @CreateDate: 2021/1/21 15:49
 * @Version: 1.0
 */
@Component
@RabbitListener(queues = CommentConstant.ORDER_QUEUE_NAME)
public class OrderConsumer {

    /**
     * 打印日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 接受字符串方式方法
     *
     * @param msg
     */
    @RabbitHandler
    public void process(String msg, Message message, Channel channel) throws InterruptedException, IOException {
        logger.info("订单消费者接收消息:{}", msg);
        // 模拟耗时
        Thread.sleep(10000);
        // 手动 ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 接收实体对象方式方法
     *
     * @param orderInfo
     */
    @RabbitHandler
    public void process(OrderInfo orderInfo) {
        logger.info("订单消费者接收消息:{}", orderInfo);
    }
}
